using System;

//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.transaction.xaframework
{


///
/// <summary> * This is a wrapper class containing the logical log, command factory,
/// * transaction factory and resource manager. Using the static <CODE>create</CODE>
/// * method, a <seealso cref="XaResourceManager"/> will be created and the
/// * <seealso cref="XaLogicalLog"/> will be instantiated (not opened).
/// * <p>
/// * The purpose of this class is to make it easier to setup all that is needed
/// * when using the <CODE>xaframework</CODE>. Classes needs to be instantiated
/// * in a certain order and references passed between the. <CODE>XaContainer</CODE>
/// * will do that work so you only have to supply the <CODE>logical log file</CODE>,
/// * your <seealso cref="XaCommandFactory"/> and <seealso cref="XaTransactionFactory"/>
/// * implementations. </summary>
/// 
	public class XaContainer
	{
		private XaCommandFactory cf = null;
		private XaLogicalLog log = null;
		private XaResourceManager rm = null;
		private XaTransactionFactory tf = null;

///    
///     <summary> * Creates a XaContainer.
///     *  </summary>
///     * <param name="logicalLog">
///     *            The logical log file name </param>
///     * <param name="cf">
///     *            The command factory implementation </param>
///     * <param name="tf">
///     *            The transaction factory implementation </param>
///     * <param name="config"> Configuration map or null if no config needed </param>
///     
		public static XaContainer create(string logicalLog, XaCommandFactory cf, XaTransactionFactory tf, Map<object, object> config)
		{
			if (logicalLog == null || cf == null || tf == null)
			{
				throw new IllegalArgumentException("Null parameter, " + "LogicalLog[" + logicalLog + "] CommandFactory[" + cf + "TransactionFactory[" + tf + "]");
			}
			return new XaContainer(logicalLog, cf, tf, config);
		}

		private XaContainer(string logicalLog, XaCommandFactory cf, XaTransactionFactory tf, Map<object, object> config)
		{
			this.cf = cf;
			this.tf = tf;
			rm = new XaResourceManager(tf, logicalLog);
			log = new XaLogicalLog(logicalLog, rm, cf, tf, config);
			rm.setLogicalLog(log);
			tf.setLogicalLog(log);
		}

///    
///     <summary> * Opens the logical log. If the log doesn't exist a new log will be
///     * created. If the log exists it will be scaned and non completed
///     * transactions will be recovered.
///     * <p>
///     * This method is only valid to invoke once after the container has been
///     * created. Invoking this method again could cause the logical log to be
///     * corrupted. </summary>
///     
//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void openLogicalLog() throws IOException
		public virtual void openLogicalLog()
		{
			log.Open();
		}

///    
///     <summary> * Closes the logical log and nulls out all instances. </summary>
///     
		public virtual void close()
		{
			try
			{
				if (log != null)
				{
					log.Close();
				}
			}
			catch (IOException e)
			{
				Console.WriteLine("Unable to close logical log");
				e.printStackTrace();
			}
			log = null;
			rm = null;
			cf = null;
			tf = null;
		}

		public virtual XaCommandFactory getCommandFactory()
		{
			return cf;
		}

		public virtual XaLogicalLog getLogicalLog()
		{
			return log;
		}

		public virtual XaResourceManager getResourceManager()
		{
			return rm;
		}

		public virtual XaTransactionFactory getTransactionFactory()
		{
			return tf;
		}
	}
}